package com.dzl.enterprise;

import com.dzl.constant.CommonConstants;
import com.dzl.enterprise.pojo.WelfareEnterprise;
import com.dzl.enterprise.service.WelfareEnterpriseService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.ImageUtils;
import com.dzl.util.JsonResult;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * saas企业相关
 * @author liuguanjun
 * @date 2021-09-07 11:01:42
 */
@RestController
@RequestMapping("/welfareEnterprise")
public class WelfareEnterpriseController {

    @Autowired
    private WelfareEnterpriseService welfareEnterpriseService;

    /**
     * 小程序首页获取合作公司列表以及海报图
     * @author liuguanjun
     * @date 2021/09/07 11:17
     */
    @GetMapping("getHomePageInfo")
    public JsonResult<Map> getHomePageInfo() {
        //查询合作公司
        WelfareEnterprise query = new WelfareEnterprise();
        query.setStatus(CommonConstants.YES);
        query.setIsDisplayHome(CommonConstants.YES);
        List<WelfareEnterprise> list = welfareEnterpriseService.select(query);
        if(EmptyUtils.isNotEmpty(list)){
            list.forEach(welfareEnterprise -> {
                welfareEnterprise.setEnterpriseLogo(ImageUtils.setImageSrc(welfareEnterprise.getEnterpriseLogo()));
            });
        }
        //查询首页海报 id为-1表示首页海报
        WelfareEnterprise welfareEnterprise = welfareEnterpriseService.selectByPrimaryKey(-1);
        String welfareHomePagePoster = "";
        if(EmptyUtils.isNotEmpty(welfareEnterprise) && EmptyUtils.isNotEmpty(welfareEnterprise.getEnterpriseLogo()) && EmptyUtils.isNotEmpty(welfareEnterprise.getIsDisplayHome()) && welfareEnterprise.getIsDisplayHome()==CommonConstants.YES){
            welfareHomePagePoster = ImageUtils.setImageSrc(welfareEnterprise.getEnterpriseLogo());
        }
        Map map = Maps.newHashMapWithExpectedSize(2);
        map.put("welfareEnterpriseList", list);
        map.put("welfareHomePagePoster", welfareHomePagePoster);
        return JsonResult.success(map);
    }

    /**
     * 根据企业id获取企业信息
     * @param enterpriseId 企业id
     * @author liuguanjun
     * @date 2021/09/07 11:22
     */
    @GetMapping("getWelfareEnterpriseInfo")
    public JsonResult<WelfareEnterprise> getWelfareEnterpriseInfo(Integer enterpriseId) {
        if(enterpriseId == null){
            return JsonResult.failWithMsg("缺少参数[企业id:enterpriseId]");
        }
        WelfareEnterprise welfareEnterprise = welfareEnterpriseService.selectByPrimaryKey(enterpriseId);
        if(EmptyUtils.isEmpty(welfareEnterprise)){
            return JsonResult.failWithMsg("该企业不存在");
        }
        if(EmptyUtils.isNotEmpty(welfareEnterprise.getEnterpriseLogo())){
            welfareEnterprise.setEnterpriseLogo(ImageUtils.setImageSrc(welfareEnterprise.getEnterpriseLogo()));
        }else{
            welfareEnterprise.setEnterpriseLogo("");
        }
        if(EmptyUtils.isNotEmpty(welfareEnterprise.getQrcode())){
            welfareEnterprise.setQrcode(ImageUtils.setImageSrc(welfareEnterprise.getQrcode()));
        }else{
            welfareEnterprise.setQrcode("");
        }
        return JsonResult.success(welfareEnterprise);
    }
}
